package com.eam.controller;

import com.mes.entity.User;
import com.eam.service.SparePartsCollarService;
import com.mes.utils.SendMessageUtil;
import com.yy.base.page.Message;
import com.yy.base.util.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("Spareparts")
public class SparePartsCollarController {
	@Autowired
	private SparePartsCollarService sparepartscollarService;
	
	 
	//加载设备备品配件物料信息
	@ResponseBody
	@RequestMapping(value="Inquireallresult",method=RequestMethod.POST)
	public Message Inquireallresult(HttpServletRequest request){
		Message msg = new Message();
			
		msg.setData("Stocks",sparepartscollarService.getSparepartsList());
		
		//查询车间
		msg.setData("adorgname",sparepartscollarService.getEquipmenadorgtList());
		
		//查询工序
		msg.setData("processname",sparepartscollarService.getEquipmenprocessList());
		
		
		
		
		return msg;
	}
	
	//设备备品配件领用根据三个筛选条件来修改下方报表内容
	@ResponseBody
	@RequestMapping(value="Submission",method=RequestMethod.POST)
	public Message Submission(HttpServletRequest request){
		Message msg = new Message();
		
		String Epcode = request.getParameter("Epcode");
		String wlnames = request.getParameter("wlnames");
		String models = request.getParameter("models");
				
		msg.setData("conditions",sparepartscollarService.getSparepartsLists(Epcode,wlnames,models));
			
		return msg;
	}
	
	//加载库存数量
	@ResponseBody
	@RequestMapping(value="SumMaterielsresult",method=RequestMethod.POST)
	public Message SumMaterielsresult(HttpServletRequest request){
		Message msg = new Message();
				
		String toolCode = request.getParameter("toolCode").toString();   //领用的物料编码
			
		//查询当前物料的库存数量
		msg.setData("sumnumbers",sparepartscollarService.getSumMateriels(toolCode));
		
		//查询该物料目前在哪个储位存放
		msg.setData("Storage",sparepartscollarService.getEquipmetStoragesList(toolCode));
		
		//查询供应商
		msg.setData("Supplier",sparepartscollarService.getEquipmetSupplierList(toolCode));
			
		return msg;
	}
	
	
	
	//查询扫描的物料编码在设备备品配件中是否存在
	@ResponseBody
	@RequestMapping(value = "getSparepartscounts", method = RequestMethod.POST)
	public Message getSparepartscounts(HttpServletRequest request){
		Message msg = new Message();
		String WULCODES = request.getParameter("toolCode").toString();   //物料编码
		
		//查询物料是否存在
		msg.setData("materiels",sparepartscollarService.getEquipmenmateriel(WULCODES));
		
		return msg;
	}
	
	//根据组织机构以及工序加载设备编码
	@ResponseBody
	@RequestMapping(value="Equipmentcode",method=RequestMethod.POST)
	public Message Equipmentcode(HttpServletRequest request){
		String ORGNAME = request.getParameter("ORGNAME");
		String GXNAME = request.getParameter("GXNAME");
				
		Message msg = new Message();
				
		msg.setData("Plans",sparepartscollarService.getEquipmentPlanList(ORGNAME,GXNAME));
		return msg;
			
	}		
	
	//根据供应商，物料编码，储位加载该储位上的物料库存数量
		@ResponseBody
		@RequestMapping(value="EquipmentVendor",method=RequestMethod.POST)
		public Message EquipmentVendor(HttpServletRequest request){
			String KWNAME = request.getParameter("KWNAME");
			//String GYSIDS = request.getParameter("GYSIDS");
			String WLCODE = request.getParameter("WLCODE");
					
			Message msg = new Message();
					
			msg.setData("vendors",sparepartscollarService.getEquipmentVendorList(KWNAME,WLCODE));
			return msg;
				
		}		
	
	 //确认完成物料领用
  	@ResponseBody
	@RequestMapping(value="SparepartsCleanReport",method=RequestMethod.POST)
  	public Message SparepartsCleanReport(HttpServletRequest request){
  		
  		Message msg = new Message();
  		
  		try {
			
		
  		String MATERIELIDS = request.getParameter("MATERIEL").toString();   //领用的物料ID
  		//String LOCATOR = request.getParameter("LOCATOR").toString();   //领用的物料所属库位
  		String company = request.getParameter("COMPANYS").toString();   //单位
  		String GONGXUID = request.getParameter("GONGXU").toString();   //领用的物料所用到工序的ID
		String processName = request.getParameter("processName");		//工序名称
		String facCodeAndName = request.getParameter("facCodeAndName");//设备编码名称
  		String SHEBEIID = request.getParameter("SHEBEI").toString();   //领用的物料所用到设备的ID
  		String BEIZHUS = request.getParameter("BEIZHUS").toString();   //备注
  		String COLLAR = request.getParameter("COLLAR").toString();   //获取领用数量的值;
  		String KUSHULIANG = request.getParameter("KUSHULIANG").toString();   //当前库存数量
  		String ANNUMBERs = request.getParameter("ANNUMBER").toString();   //安全库存数量
  		String ORGIDS = request.getParameter("ORGIDS").toString();   //使用的物料所属的车间
  		String orgName = request.getParameter("orgName");   //使用的物料所属的车间名称
  		String WULNAMES = request.getParameter("WULNAMES").toString();   //物料名称
  		String WULCODES = request.getParameter("WULCODES").toString();   //物料编码
  		String CHUWEIS = request.getParameter("CHUWEIS").toString();   //储位ID
  		String GYSNAMES = request.getParameter("GYSNAMES").toString();   //供应商
  		
  		
  		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		Date time  =sdf.parse(sdf.format(new Date())); //获取当前系统时间（用于出库时间）
		
		User user = UserUtil.getLoginUser(request);
  		String userIdname = user.getId();//登录人的ad_user_id（领用人）
  		String adclientId = user.getClientId();   //登录人的ad_client_id
  		
  		List<Map<String, Object>> ListUser = sparepartscollarService.getEquipmentputCreateby(userIdname);
  		
  		String createBy = ListUser.get(0).get("CREATEDBY").toString();  //登录人的CreateBy
		
		String updatedBy = ListUser.get(0).get("UPDATEDBY").toString(); //登录人的updateBy
  		
		
		//获取当前出库的物料的价格
		List<Map<String, Object>> ListQTYS = sparepartscollarService.getEquipmentQtys(MATERIELIDS,CHUWEIS);
		
		String JIAGE = "";
		
		if(ListQTYS.get(0).get("PRICES") == null){
			
		}else{
			JIAGE = ListQTYS.get(0).get("PRICES").toString();
		}
		
		
		String companys  = "";
  		
  		if( company.equals("对")){
			companys= "10";
		}else if( company.equals("个")){
			companys= "20";
		}else if( company.equals("根")){
			companys= "30";
		}else if( company.equals("盒")){
			companys= "40";
		}else if( company.equals("卷")){
			companys= "50";
		}else if( company.equals("块")){
			companys= "60";
		}else if( company.equals("片")){
			companys= "70";
		}else if( company.equals("套")){
			companys= "80";
		}
		
  		//设备出入库类型判读为出库
  		String type = "Out";
  		
  		String status = "Audit";  //设备备品配件领用记录为待审核
  		
  		String last = null;
  		
  		int COLLARs = Integer.parseInt(COLLAR);      //转化后的领用数量
  		int KUSHULIANGs = Integer.parseInt(KUSHULIANG); //转化后的库存数量
  		int price = 0;
  		if(JIAGE == ""){
  			
  		}else{
  			price = Integer.parseInt(JIAGE); //转化后的价格
  		}
  		
  		int ANNUMBER = Integer.parseInt(ANNUMBERs); //转化后的安全库存数量
  		
  		int deduction = KUSHULIANGs - COLLARs; //领用后还剩下的库存数量
  		
  		//从物料清单库存中扣除领用数量
  		//sparepartscollarService.getUpdaeteSpareParts(deduction,MATERIELIDS);
  		
  		
  		//将出库信息存入设备备品配件出入库记录表中
  		boolean result = sparepartscollarService.insertSparePartsList(MATERIELIDS,CHUWEIS,companys,COLLARs,price,GONGXUID,SHEBEIID,time,userIdname,type,status,BEIZHUS,adclientId,ORGIDS,createBy,updatedBy,KUSHULIANGs,GYSNAMES);
  		//发送备品配件提醒消息
  		if(result) {
  			String msgContent =  "设备备品配件领用：\r\n"
								+"车间："+orgName+" 工序："+processName+"\r\n"
								+"设备编码名称："+facCodeAndName+"\r\n"
								+"物料编码："+WULCODES+" 物料名称：" + WULNAMES +"\r\n"
								+"剩余库存："+deduction+company+" 领用数量：" + COLLARs +company+ " 领用人：" + user.getName()+"\r\n"
								+"领用说明："+BEIZHUS;
  			SendMessageUtil.sendMessage("SBBPPJLY", "设备备品配件领用", msgContent,ORGIDS,"");
  		}
  		
//  		if(deduction <= ANNUMBER){
//  			//触发消息提醒功能
//  			SendMessageUtil.sendMessage("SPARE_CODE", "设备备品配件物料清单，物料编码为"+WULCODES+"开始提醒", "物料名称为："+WULNAMES+"的物料库存到达安全库存数量,请尽快补充物料！！");
//  		}
  		
  		
  		} catch (Exception e) {
			// TODO: handle exception
		}
  		return msg;
  		
  		
  	}
	
	
	
	
	
	
	
	
}
